import pandas as pd
import re

# =====变量定义======================
# 颜色映射表存储，dict类型
data_table = {}
#碎花模式匹配
floralPat = re.compile(r'FL(\d)N')

# ======逻辑=========================
# 模块初始化
def init(data_src):
    df = None
    if type(data_src) == str:
        src_xls = pd.ExcelFile(data_src)
        df = src_xls.parse(sheet_name='colorcode')
    else:
        df = data_src
    # 开始解析表格，转换到dict里
    df.set_index('code')
    temp = df.to_dict('records')
    for row in temp:
        data_table[row['code']] = {'name': row['name'], 'cate': row['cate'], 'zhname': row['zhname']}

# 获取值，参数：code, other_data_col_name:可选3个值'name','cate','zhname'
def getVal(code, col_name):
    rt_str = None
    # 解析颜色代码是否属于FLXN模式（碎花印花，比如 FL1N，代表碎花1型）
    rs = floralPat.match(code)
    if rs != None:
        Nodot = rs.group(1)
        if col_name == 'name':
            rt_str = f"Floral #{Nodot}"
        elif col_name == 'cate':
            rt_str = 'Multi-color'
        elif col_name == 'zhname':
            rt_str = f"碎花{Nodot}型"
        else:
            rt_str = None
    else:
        if code in data_table:
            rt_str = data_table[code][col_name]
        else:
            rt_str = None
    return rt_str

